package com.hicoffee.bm.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by rayl on 2017/7/12.
 */
public class DateUtil extends com.radarwin.framework.util.DateUtil {

    public static final String YYYY_MM = "yyyy-MM";  //format like as "2017-08"
    public static final String YYYY__MM = "yyyy/MM";  //format like as "2017/08"

    public static String getYearStr(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return String.valueOf(calendar.get(Calendar.YEAR));
    }

    public static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH);
    }

    public static String getStrDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(date);
    }


    public static String getMonthAsString(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return String.valueOf(calendar.get(Calendar.MONTH) + 1);
    }

    public static String formatDateString(String dateAsString, String formatter) {
        Date date = convertToDate(dateAsString, formatter);
        return convertToString(date, formatter);
    }

    public static String formatDateString(String dateAsString, String nowFormatter, String originalFormatter) {
        Date date = convertToDate(dateAsString, originalFormatter);
        return convertToString(date, nowFormatter);
    }


    public static Map<String, String> getNextMonth(Date date) {
        Map<String, String> result = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        result.put("year", String.valueOf(calendar.get(Calendar.YEAR)));
        result.put("month", String.valueOf(calendar.get(Calendar.MONTH) + 1));
        return result;
    }

    public static Map<String, String> getPrevious(String year, String month) {
        Map<String, String> result = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Integer.valueOf(year));
        calendar.set(Calendar.MONTH, Integer.valueOf(month) - 1);
        calendar.add(Calendar.MONTH, -1);
        result.put("year", String.valueOf(calendar.get(Calendar.YEAR)));
        result.put("month", String.valueOf(calendar.get(Calendar.MONTH) + 1));
        return result;
    }

    public static void main(String[] args) {
        Map<String, String> result = getPrevious("2017","1");
        System.out.println(result.get("year"));
        System.out.println(result.get("month"));
    }
}
